package com.moleq.adapter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.moleq.mgpos.activities.R;
import com.moleq.models.TextAndCheckBox;

public class TextAndCheckboxListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private int selectedRow = -1;
	public CheckBox cb_check;
	private List<TextAndCheckBox> listItem;

	public TextAndCheckboxListAdapter(Context context, List<TextAndCheckBox> listItem) {
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
	}
	
	public void SetSource(List<TextAndCheckBox> listItem){
		this.listItem = listItem;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.permissions_list_item,null);
			holder.fs_name = (TextView) convertView.findViewById(R.id.tv_permissions_name);
			holder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		String ls_fs_name = String.valueOf(listItem.get(position).getText());
		holder.fs_name.setTextSize(16);
		holder.fs_name.setText(ls_fs_name);

		holder.cb_check.setChecked(false);
		if (listItem.get(position).isCheck()) {
			holder.cb_check.setChecked(true);
		}

		holder.cb_check.setTag(position);
		holder.cb_check.setOnCheckedChangeListener(onCheckedChangeListener);

		return convertView;
	}

	public OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			int index = (Integer) buttonView.getTag();
			listItem.get(index).setCheck(isChecked);
		}
	};

	public final class ViewHolder {
		public TextView fs_name;
		public CheckBox cb_check;
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}
};
